﻿#include "SF2Region.h"
using namespace yzrilyzr_collection;
using namespace yzrilyzr_util;
using namespace yzrilyzr_array;
namespace yzrilyzr_soundbank{
	SF2Region::SF2Region(){
		generators=std::make_shared<HashMap<int32_t, int16_t>>();
		modulators=std::make_shared<ArrayList<std::shared_ptr<SF2Modulator>>>();
	}
	SF2Region::~SF2Region(){
		generators.reset();
		modulators.reset();
	}
	std::shared_ptr<HashMap<int32_t, int16_t>> SF2Region::getGenerators(){
		return generators;
	}
	std::shared_ptr<ArrayList<std::shared_ptr<SF2Modulator>>> SF2Region::getModulators(){
		return modulators;
	}
	void SF2Region::putShort(int32_t generator, int16_t value){
		generators->put(generator, value);
	}
	ByteArray * SF2Region::getBytes(int32_t generator){
		int val=getInteger(generator);
		ByteArray * bytes=new ByteArray(2);
		(*bytes)[0]=(int8_t)(0xFF & val);
		(*bytes)[1]=(int8_t)((0xFF00 & val) >> 8);
		return bytes;
	}
	int32_t SF2Region::getInteger(int32_t generator){
		return 0xFFFF & getShort(generator);
	}
	int16_t SF2Region::getShort(int32_t generator){
		if(!contains(generator))
			return getDefaultValue(generator);
		auto res= generators->get(generator);
		return res.has_value()?res.value():0;
	}
	bool SF2Region::contains(int32_t generator){
		return generators->containsKey(generator);
	}
	int16_t SF2Region::getDefaultValue(int32_t generator){
		if(generator == 8) return (int16_t)13500;
		if(generator == 21) return (int16_t)-12000;
		if(generator == 23) return (int16_t)-12000;
		if(generator == 25) return (int16_t)-12000;
		if(generator == 26) return (int16_t)-12000;
		if(generator == 27) return (int16_t)-12000;
		if(generator == 28) return (int16_t)-12000;
		if(generator == 30) return (int16_t)-12000;
		if(generator == 33) return (int16_t)-12000;
		if(generator == 34) return (int16_t)-12000;
		if(generator == 35) return (int16_t)-12000;
		if(generator == 36) return (int16_t)-12000;
		if(generator == 38) return (int16_t)-12000;
		if(generator == 43) return (int16_t)0x7F00;
		if(generator == 44) return (int16_t)0x7F00;
		if(generator == 46) return (int16_t)-1;
		if(generator == 47) return (int16_t)-1;
		if(generator == 56) return (int16_t)100;
		if(generator == 58) return (int16_t)-1;
		return 0;
	}
	void SF2Region::putBytes(int32_t generator, ByteArray & bytes){
		putShort(generator, (bytes[0] + (bytes[1] << 8)));
	}
	void SF2Region::putInteger(int32_t generator, int32_t value){
		putShort(generator, value);
	}
}